// 全局事件控制器

export interface EventInfo {
  eventName: string;
  call: any;
  func: Function;
}

export class EventManager {
  private _eventArr: EventInfo[] = [];

  public on(eventName: string, call: unknown, func: Function) {
    this._eventArr.push({ eventName, call, func });
  }
  public off(eventName: string, func: Function | null = null) {
    this._eventArr = this._eventArr.filter((e) => {
      if (func) {
        return !(eventName === e.eventName && func === e.func);
      } else {
        return !(eventName === e.eventName);
      }
    });
  }
  public emit(eventName: string) {
    this._eventArr.forEach((e) => {
      if (e.eventName === eventName) {
        e.func.call(e.call);
      }
    });
  }
}
